#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


static int i = 0;

void f_write();
void f_read();

void o_write();
void o_read();

int main(void) {
//	f_write();
//
//	f_read();
	
//	o_write();

//	o_read();

//    char* aa = (char*)malloc(2);
//
//    printf("%d\n",aa);
//
//    strcpy(aa,"dd");
//
//    printf("%d\n",aa);
//
//    printf("%s\n",aa);


	FILE * fp;
	char buffer[1024];
	fp=popen("df","r");
	char* s = NULL;
	while((s=fgets(buffer,sizeof(buffer),fp))!=NULL){
		printf("%s" , buffer);
	}

	pclose(fp);

	return 1;
}

void o_write() {
	int file = open("time_log.txt", O_RDWR|O_CREAT, 0777);
	if (file >= 0) {
		time_t t;
		time(&t);
		char* time_s = asctime(gmtime(&t));
		char is[128];
		char* ss = (char*) malloc(sizeof(char) * 128);
		while (1) {
			i++;
			sprintf(is, "%d", i);
			ss = strcpy(ss, is);
			ss = strcat(ss, "   ");
			ss = strcat(ss, time_s);
			write(file, ss, strlen(ss));
			if (i > 3) {
				break;
			}
		}
		free(ss);
	}else{
		perror("read STDIN_FILENO");
		exit(1);
	}
	
	close(file);

}

void o_read() {
	int file =  open("time_log.txt", O_RDWR|O_CREAT, 0777);
	if(file>0){
		char* ss = (char*) malloc(sizeof(char) * 128);
		ssize_t k;
		lseek(file,9,SEEK_SET);
		while((k=read(file,ss,128))>0){
			printf("%s\n",ss);
		}
		free(ss);
	}else{
		perror("zekezang");
		exit(1);
	}
	close(file);
	
}

//ffile 写
void f_write() {
	FILE* file = fopen("time_log.log", "a+");
	time_t t;
	time(&t);
	char* time_s = asctime(gmtime(&t));

	char is[128];
	char* ss = (char*) malloc(sizeof(char) * 128);
	while (1) {
		i++;
		//			sleep(2);
		sprintf(is, "%d", i);
		ss = strcpy(ss, is);
		ss = strcat(ss, "   ");
		ss = strcat(ss, time_s);
		fputs(ss, file);
		fflush(file);
		if (i > 3) {
			break;
		}
	}
	//		free(time_s);
	free(ss);
	fclose(file);
}

//ffile 读
void f_read() {
	FILE* file = fopen("time_log.log", "r");
	char* ss = (char*) malloc(sizeof(char) * 128);
	while (fgets(ss, 128, file) != NULL) {
		printf("%s", ss);
	}
	free(ss);
	fclose(file);
}
